<?php
error_reporting(E_ALL & ~E_NOTICE);
date_default_timezone_set("Asia/Shanghai");

header("Cache-Control: no-cache");
header("Content-Type: text/event-stream");

$counter = rand(1, 3);
while (true) {
    echo "event: ping\n";
    $curDate = date(DATE_ATOM);
    echo 'data: {"time": "' . $curDate . '"}';
    echo "\n\n";

    $counter--;

  if (!$counter) {
      echo 'data: This is a message at time ' . $curDate . "\n\n";
      $counter = rand(1, 3);
  }

    ob_end_flush();
    flush();

    sleep(1);
}